<?php

namespace App\Http\Controllers\admin;


use App\Http\Model\Categroy;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

/**
 * Class CateController 后台栏目控制器
 * @package App\Http\Controllers\admin
 */
class CateController extends AdminPlatformController
{
    /** 属性声明 */
    private $error = null;
    private $cat_model = null;

    /** 方法声明 */
    /**
     * 构造方法
     */
    public function __construct(Route $route){
        parent::__construct($route);
        $this -> cat_model = new Categroy();
    }

    /**
     * 栏目列表展示
     */
    public function cate_list(){
        $cat = new Categroy();
        $data = $cat->orderBy('cat_id','asc')->get();
        $data = $cat->getTree($data);
        return view('admin.category.categoryList',compact('data'));
    }
    /**
 * 栏目列表添加
 */
    public function cate_add(){
        $cat = new Categroy();
        $data = Categroy::all();
        $arr = $cat->getTree($data);
        return view('admin.category.categoryAdd',compact('arr'));
    }
    /**
     * 提交栏目添加
     */

    public function postcate_add(){
        $input = Input::except('_token');
        $rlu = [
            'cat_name'=>'required|max:20|min:1',
            'sort_order'=>'required|numeric'
        ];
        $mess = [
            'cat_name.required'=>'栏目名称不能为空',
            'cat_name.max'=>'栏目最多20个字',
            'cat_name.min'=>'栏目最少1个字',
            'sort_order.required'=>'排序不能为空',
            'sort_order.numeric'=>'排序必须为数字'
        ];
        $validate = Validator::make($input,$rlu,$mess);
        if($validate->passes()){
            $rs = Categroy::create($input);
            if($rs){
                return redirect('manager/cate');
            }else{
                return back()->with('errors','添加失败稍后再试')->withInput();
            }
        }else{
            return back()->withErrors($validate);
        }
    }
    /**
     * 栏目列表删除
     * @param $id int 栏目主键ID
     */
    public function cate_del($id){
        $cat = new Categroy();
        if($cat->find($id)->delete()){
            if($cat->parent_id == 0){
                $cat->update(['parent_id'=>$id]);
            }
           return redirect('manager/cate');
        }else{
            return back()->with('errors','删除失败稍后再试');
        }
    }
    /**
     * 栏目列表编辑
     * @param $id int 栏目主键ID
     */
    public function cate_edit($id){
        $cat = new Categroy();
        $data = Categroy::all();
        $arr = $cat->getTree($data);


        $data = Categroy::where('cat_id',$id)->first();
        //dd($data);
        return view('admin.category.categoryEdit',compact('data','arr'));
    }
    /**
     * 栏目列表编辑提交到数据库
     * @param $id int 栏目主键ID
     */
    public function postcate_edit($id){
        $input = Input::except('_token');
        $rlu = [
            'cat_name'=>'required|max:20|min:4',
            'sort_order'=>'required|numeric'
        ];
        $mess = [
            'cat_name.required'=>'栏目名称不能为空',
            'cat_name.max'=>'栏目最多20个字',
            'cat_name.min'=>'栏目最少4个字',
            'sort_order.required'=>'排序不能为空',
            'sort_order.numeric'=>'排序必须为数字'
        ];
        $validate = Validator::make($input,$rlu,$mess);
            if($validate->passes()){
                /*业务逻辑判断*/
                /** 1.栏目上级id不能是自身id */
                if($id == $input['parent_id']){
                    $this -> error = "栏目上级id不能是自身栏目id!";
                    return back()->with('errors',$this -> error);
                }
                /** 2.该栏目的上级id 不能是自身子栏目树下的cat_id */
                if($re = $this -> cat_model -> checkId($input,$id)){
                    $this -> error = "栏目的上级id 不能是自身子栏目树下的栏目id!";
                    return back()->with('errors',$this -> error);
                }

                $rs = Categroy::where('cat_id',$id)->update($input);
                if($rs){
                    return redirect('manager/cate');
                }else{
                    return back()->with('errors','添加失败稍后再试');
                }
            }else{
                return back()->withErrors($validate);
            }
    }
}
